<?php

namespace App\Repository\Impl;

if (!defined('IN_PX'))
    exit;

use App\Repository\Member;
use PDO;

class MemberImpl implements Member {

    //仓储层组件
    private function __Repository($value = 'member') {}

    private function __Inject($db) {}

    private $_table = '`#@__@member`';

    public function findMember($memberId, $type = 0) {
        if ($type == 0) {
            $_field = is_numeric($memberId) ? 'mobile' : 'member_name';
        } else if ($type == 1) {
            $_field = 'member_id';
        } else if ($type == 2) {
            $_field = 'openid';
        } else {
            $_field = 'member_id';
        }
        $this->db->debug();
        return $this->db->select('*')
            ->table($this->_table)
            ->where("`{$_field}` = ?")
            ->bind(array($memberId))
            ->find();
    }

    public function findMemberOpenid($mobile, $openid) {
        return $this->db->select('*')
            ->table($this->_table)
            ->where('`mobile` = ? AND `openid` = ?')
            ->bind(array($mobile, $openid))
            ->find();
    }

    public function save($post) {
        $this->db->debug();
        return $this->db->table($this->_table)
            ->row(array(
                'member_name' => '?',
                'mobile' => '?',
                'sex' => '?',
                'department_id' => '?',
                'location' => '?',
                'is_status' => '?',
                'password' => '?',
                'guid' => '?',
                'add_date' => '?',
                'release_date' => '?',
                'last_log_date' => '?',
                'last_log_ip' => '?',

                'portrait' => '?',
                'openid' => '?',
                'nickname' => '?',
                'headimgurl' => '?',
            ))
            ->bind($post)
            ->save();
    }

    public function update($aryUpdateRow = array(), $aryUpdateBind = array()) {
        $this->db->debug();
        return $this->db->table($this->_table)
            ->row($aryUpdateRow)
            ->where('`member_id` = ?')
            ->bind($aryUpdateBind)
            ->update();
    }

    public function chkExists($mobile) {
        return $this->db->table($this->_table)->where('`mobile` = ?')->bind(array($mobile))->exists();
    }

    public function chkExists2($mobile) {
        return $this->db->table($this->_table)->where('`mobile` = ? AND `openid` != \' \'')->bind(array($mobile))->exists();
    }

}
